

/*
File: eqemuTelnet.java
Copyright (C) 2010: Ronny L. Bull (aka: Cubber)
Date: 9-03-2010
Development OS: Gentoo Linux
Java Version: sun-jdk-1.6.0.20
Description: EQEmu Java Server Manager - Telnet Functions

License:

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY except by those people which sell it, which
are required to give you total support for your newly bought product;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
Code Adaption Notice:

This code was adapted from the sample found here:
http://www.informit.com/guides/content.aspx?g=java&seqNum=40
*/

/*
Dependency Notice:

This code requires the apache commons package that can be found here:
http://commons.apache.org/net/
*/

package eqemujsm.telnet;

//Imports
import org.apache.commons.net.telnet.*;
import java.io.*;

public class eqemuTelnet
{

	//Variables
	private	TelnetClient telnet = new TelnetClient();
	private	InputStream in;
	private	PrintStream out;
	private	char prompt = '>';


		public eqemuTelnet( String server, int port, String user, String password )
		{
			try
			{

				//Connect to the server
				telnet.connect(server, port);

				//Get input and output stream references
				in = telnet.getInputStream();
				out = new PrintStream( telnet.getOutputStream() );

				//Log in user
				readUntil( "Username: ");
				write( user );
				readUntil( "Password: ");
				write( password );

				//Advance to prompt
				readUntil( prompt + " " );

			}
			catch ( Exception e )
			{
				e.printStackTrace();
			}
		}

		public String readUntil( String pattern )
		{
			try
			{
				char lastChar = pattern.charAt( pattern.length() - 1 );
				StringBuffer sb = new StringBuffer();
				char ch = ( char )in.read();
				while( true )
				{
					System.out.print( ch );
					sb.append( ch );
					if( ch == lastChar )
					{
						if( sb.toString().endsWith( pattern ) )
						{
							return sb.toString();
						}
					}
					ch = ( char )in.read();
				}
			}
			catch ( Exception e )
			{
				e.printStackTrace();
			}
			return null;
		}

		public void write( String value )
		{
			try
			{
				out.println( value );
				out.flush();
				System.out.println( value );
			}
			catch ( Exception e )
			{
				e.printStackTrace();
			}
		}

		public String sendCommand( String command ) 
		{
   			try 
			{
	 			write( command );
	 			return readUntil( prompt + " " );
   			}
   			catch( Exception e ) 
			{
	 			e.printStackTrace();
   			}
   			return null;
  		}

		public void disconnect() 
		{
   			try 
			{
	 			telnet.disconnect();
   			}
   			catch( Exception e ) 
			{
	 			e.printStackTrace();
   			}
  		}
}
